var app = getApp()
// pages/order_list/order_list.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    selected: true,
    selected1: false,
    selected2: false,
    selected3: false,
    selected4: false,
    status: '',
    order_list: [],
    firstRow: 0,
    orderkey: '',
    k: '',
    u: '',
    request_num: '',//加载底部使用
    show: 0,
    is_show_time: 1,
    search_show: false,
    order_status:''
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var status = options.status
    var key = wx.getStorageSync('key')
    var uid = wx.getStorageSync('u')
    var firstRow = that.data.firstRow
    that.setData({
      k: key,
      u: uid,
    })
    var data = { k: key, u: uid};
    console.log(status)
    if (status){
      data = { k: key, u: uid, status: status }
      that.setData({
        order_status: status
      })
    }
    if (key) {
      wx.request({
        url: app.configData.shop.api_url + '/index.php?ctl=Distribution_Buyer_Directseller&met=directsellerOrder&typ=json&firstRow=' + firstRow,
        method: 'GET',
        data: data,
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
         
          that.setData({
            order_list: res.data.data.items,
            is_show_time: 0
          })
        }
      })
    } else {
      that.setData({
        is_show_time: 0
      })
    }
  },

 
  //订单列表切换
  order: function (e) {
    var that = this;
    var status = e.currentTarget.dataset.status
    var firstRow = 0
    switch (status) {
      case 'all':
        that.setData({
          selected: true,
          selected1: false,
          selected2: false,
          selected3: false,
          selected4: false,
        })
        break;
      case 'wait_pay':
        that.setData({
          selected: false,
          selected1: true,
          selected2: false,
          selected3: false,
          selected4: false,

        })
        break;
      case 'order_payed':
        that.setData({
          selected: false,
          selected1: false,
          selected2: true,
          selected3: false,
          selected4: false,
        })
        break;
      case 'finish':
        that.setData({
          selected: false,
          selected1: false,
          selected2: false,
          selected3: true,
          selected4: false,
        })
        break;
      case 'cancel':
        that.setData({
          selected: false,
          selected1: false,
          selected2: false,
          selected3: false,
          selected4: true,
        })
        break;
    }
    if (that.data.k) {
      wx.request({
        url: app.configData.shop.api_url + '/index.php?ctl=Distribution_Buyer_Directseller&met=directsellerOrder&typ=json&firstRow=' + firstRow,
        method: 'GET',
        data: { k: that.data.k, u: that.data.u, status: status, orderkey: that.data.orderkey },
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {

          that.setData({
            order_list: res.data.data.items,
            status: status,
            firstRow: firstRow,
            request_num: '',
            is_show_time: 0
          })

        }
      })
    }
  },
  //获取用户输入的用户名
  contentInput: function (e) {
    if (e.detail.value) {
      this.setData({
        orderkey: e.detail.value,
        search_show: true
      })
    } else {
      this.setData({
        search_show: false
      })
    }
  },
  //事件处理函数
  bindViewTap: function () {
    var that = this;
    var orderkey = that.data.orderkey
    if (orderkey == undefined) {
      orderkey = ''
    }
    var status = that.data.status

    var firstRow = 0
    if (that.data.k) {
      wx.request({
        url: app.configData.shop.api_url + '/index.php?ctl=Distribution_Buyer_Directseller&met=directsellerOrder&typ=json&firstRow=' + firstRow,
        method: 'GET',
        data: { k: that.data.k, u: that.data.u, status: status, orderkey: orderkey },
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          that.setData({
            order_list: res.data.data.items,
            firstRow: firstRow,
            request_num: '',
            is_show_time: 0
          })
        }
      })
    }

  },


  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this
    if (that.data.k) {
      var firstRow = that.data.firstRow + 10
      var status = that.data.status
      
      var orderkey = that.data.orderkey
      if (orderkey == undefined) {
        orderkey = ''
      }
      
      if (that.data.request_num == 1) {
        return false
      }
    
      wx.request({
        url: app.configData.shop.api_url + '/index.php?ctl=Distribution_Buyer_Directseller&met=directsellerOrder&typ=json&firstRow=' + firstRow,
        method: 'GET',
        data: { k: that.data.k, u: that.data.u, status: status, orderkey: orderkey },
        header: {
          'content-type': 'application/json'
        },
        success: function (res) {
          
          if (res.data.data.items.length > 0) {
            var order_list = that.data.order_list
            for (var z in res.data.data.items) {
              order_list.push(res.data.data.items[z])
            }
            that.setData({
              order_list: order_list,
              firstRow: firstRow,
              is_show_time: 0
            })
          } else {
            that.setData({
              request_num: 1,
              is_show_time: 0
            })
          }
        }
      })
    }
  },
  bindhref: function (e) {
    app.goTop()
    this.setData({
      show: 0
    })
  },
  //一键到顶
  onPageScroll: function (e) {
    // 获取滚动条当前位置
    var that = this
    wx.getSystemInfo({
      success: function (res) {
        if (e.scrollTop + res.windowHeight > res.windowHeight) {
          that.setData({
            show: 1
          })
        } else {
          that.setData({
            show: 0
          })
        }
      },
    })
  },
  //删除搜索框内容
  search_clear: function () {
    var that = this
    that.setData({
      orderkey: '',
      search_show: false
    })
  },
  //下拉刷新
  onPullDownRefresh: function () {
    var that = this
    wx.showToast({
      title: '加载中',//提示信息
      icon: 'loading',//成功显示图标
      duration: 2000//时间
    })
    wx.stopPullDownRefresh()
    that.onLoad()
  },
})